package com.qidian.tcba.admin.service.impl;

import com.qidian.tcba.admin.model.dto.district.DistrictListDTO;
import com.qidian.tcba.admin.model.dto.user.UserOrgMsgDTO;
import com.qidian.tcba.admin.model.entity.UserExtendMsg;
import com.qidian.tcba.admin.model.param.UserOrgMsgParam;
import com.qidian.tcba.admin.service.UserDistrictService;
import com.qidian.tcba.admin.service.UserExtendMsgService;
import com.qidian.tcba.dao.OrgnizationMapper;
import com.qidian.tcba.dao.UserExtendMsgMapper;
import com.qidian.tcba.oss.service.AttachmentsService;
import com.qidian.tcba.utils.BeanMapper;
import com.qidian.tcba.utils.TablesName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户扩展信息表 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-07-06
 */
@Service
public class UserExtendMsgServiceImpl implements UserExtendMsgService {

    @Autowired
    private UserExtendMsgMapper userExtendMsgMapper;

    @Autowired
    private AttachmentsService attachmentsService;

    @Autowired
    private UserDistrictService userDistrictService;

    @Override
    public UserOrgMsgDTO getUserOrgMsg(int userId) {
        UserExtendMsg userOrgMsg = userExtendMsgMapper.getUserOrgMsg(userId);
        if (userOrgMsg == null) {
            return null;
        }
        List<DistrictListDTO> userDistrictList = userDistrictService.getUserDistrictList(userId);
        UserOrgMsgDTO map = BeanMapper.map(userOrgMsg, UserOrgMsgDTO.class);
        map.setUserDistrictList(userDistrictList);
        return map;
    }

    @Transactional
    @Override
    public boolean completeOrgMsg(UserOrgMsgParam userOrgMsgParam) {
        UserExtendMsg map = BeanMapper.map(userOrgMsgParam, UserExtendMsg.class);
        int i = userExtendMsgMapper.insertOrgMsg(map);
        List<Integer> fileId = new ArrayList<>();
        fileId.add(userOrgMsgParam.getFileId());
        //关联用户扩展表的营业执照
        attachmentsService.relateAttach(map.getId(), TablesName.USER_EXTEND_MSG,fileId);
        //插入用户区县列表
        userDistrictService.deleteUserDistrictList(userOrgMsgParam.getUserId());
        userDistrictService.insertUserDistrictList(userOrgMsgParam.getUserId(), userOrgMsgParam.getUserDistrictIds());
        return true;
    }
}
